using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Codeflows;
using Codeflows.Results;

namespace CodeflowsTests
{
    public static class WhenCodeflow
    {
        public static CodeflowRunResult<T> Executed<T>(Expression<Func<T, Result>> startAction) where T : Codeflow
        {
            return Executed(startAction, TimeSpan.Zero);
        }

        public static CodeflowRunResult<T> Executed<T>(Expression<Func<T, Result>> startAction, TimeSpan timeout) where T : Codeflow
        {
            var instance = Activator.CreateInstance<T>();
            var codeflowId = CodeflowApplication.Create(startAction);
            var result = instance.Run(codeflowId, timeout);
            return new CodeflowRunResult<T>(Tracker.InMemory, instance, codeflowId, result);
        }

        public static CodeflowRunResult<T> AsynchronouslyExecuted<T>(Expression<Func<T, Task<Result>>> startAction) where T : Codeflow
        {            
            var instance = Activator.CreateInstance<T>();
            var codeflowId = CodeflowApplication.Create(startAction);
            var result = instance.Run(codeflowId);
            return new CodeflowRunResult<T>(Tracker.InMemory, instance, codeflowId, result);
        }
    }
}